package com.sucker.infoservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sucker.commonutils.R;
import com.sucker.infoservice.entity.Achievement;
import com.sucker.infoservice.service.AchievementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 成果表
 * </p>
 *
 * @author sucker
 * @since 2022-07-04
 */
@Tag(name = "AchievementController",description = "成果管理")
@RestController
@RequestMapping("/infoservice/achievement")
@CrossOrigin
public class AchievementController {

    @Autowired
    private AchievementService achievementService;

    /**
     * 查询成果数量
     * @param achName
     * @return
     */
    @Operation(summary = "查询成果数量",description = "根据成果名查询成果数量")
    @GetMapping("queryByachName")
    public R queryByachName(@Parameter(description = "成果名") @RequestParam String achName){

        Achievement achievement = achievementService.getOne(new QueryWrapper<Achievement>().eq("ach_name", achName));
        if(ObjectUtils.isEmpty(achievement)) return R.error().message("未查询到相关内容！");

        return R.ok().data("achievement",achievement);

    }


    /**
     * 查询所有成果数量
     * @return
     */
    @Operation(summary = "查询所有成果数量")
    @GetMapping("queryAll")
    public R queryAll(){
        List<Achievement> list = achievementService.list();
        HashMap<String, String> map = new HashMap<>();
        list.forEach(achievement -> {
            map.put(achievement.getAchName(),achievement.getQuantity());
        });
        return R.ok().data("map",map);
    }

}

